import { _decorator, CCFloat, Component, Label, Node, Sprite, tween, Vec3 } from 'cc';
import { CarColorArr, GAME_PREFAB, GlobalData, SOUND_TYPE } from '../../../Data/GlobalData';
import { Tools } from '../../../Tool/Tools';
const { ccclass, property } = _decorator;

@ccclass('SingleCar')
/**
 * 单个汽车
 */
export class SingleCar extends Component {

    //车
    @property(Sprite)
    private Car: Sprite = null;

    //车的类型
    @property(CCFloat)
    public CarType: number = 0;

    //子弹数量
    public BulletCount: number = 0;

    //车的颜色值
    @property(CCFloat)
    public CarColorIndex: number = 0;

    //车的宽高
    @property(CCFloat)
    public CarWidth: number = 0;
    @property(CCFloat)
    public CarHeight: number = 0;

    public CheckWidth: number = 0;
    public CheckHeight: number = 0;

    //子弹数量文字
    @property(Label)
    private ButtleLabel: Label = null;

    //移动速度
    private MoveSpeed: Vec3 = new Vec3(0, 0);

    //起始位置
    private StartPos: Vec3 = new Vec3(0, 0, 0);

    //回调函数
    public Cb: Function = null;

    //碰撞效果
    @property(Node)
    private CollisionEffect: Node = null;

    //赛车移动速度
    public CarMoveSpeed: number = 20;

    protected onLoad(): void {
        this.CollisionEffect.active = false;
        this.CollisionEffect.setScale(new Vec3(0.8, 0.8, 0.8));
        this.StartPos = this.node.getPosition();
        this.Car.spriteFrame = app.resManager.SpriteFrames[GAME_PREFAB.CarBody + this.CarColorIndex];
        this.BulletCount = GlobalData.CarButtle[this.CarType];
        this.ButtleLabel.string = this.BulletCount.toString();

        if (this.node.angle == 0 || this.node.angle == 180) {
            this.CheckWidth = this.CarWidth / 2;
            this.CheckHeight = this.CarHeight / 2;
        } else if (this.node.angle == 90 || this.node.angle == -90) {
            this.CheckWidth = this.CarHeight / 2;
            this.CheckHeight = this.CarWidth / 2;
        }

    }

    /**
     * 更新颜色
     */
    public UpdateColor(_colorindex: number) {
        this.Car.spriteFrame = app.resManager.SpriteFrames[GAME_PREFAB.CarBody + _colorindex];
        this.CarColorIndex = _colorindex;

    }

    /**
     * 被点击到了
     */
    public Click(): void {
        GlobalData.CanClickCar = false;
        switch (this.node.angle) {
            case 0:
                this.MoveSpeed = new Vec3(0, this.CarMoveSpeed);
                break;
            case 90:
                this.MoveSpeed = new Vec3(-this.CarMoveSpeed, 0);
                break;
            case 180:
                this.MoveSpeed = new Vec3(0, -this.CarMoveSpeed);
                break;
            case -90:
                this.MoveSpeed = new Vec3(this.CarMoveSpeed, 0);
                break;
        }
    }

    /**
     * 停止并更新炮台
     */
    public StopUpdateGun() {
        this.MoveSpeed = new Vec3(0, 0, 0);
        GlobalData.CanClickCar = true;
        GlobalData.IsHit = true;
        this.Cb && this.Cb(this.CarColorIndex, this.BulletCount);
        this.node.active = false;
    }

    /**
     * 恢复位置
     */
    public ResetPos() {
        app.audioManager.PlayEffect(SOUND_TYPE.HitCar);
        app.audioManager.StopEffect(SOUND_TYPE.CarMove);
        this.CollisionEffect.active = true;
        this.scheduleOnce(() => {
            this.CollisionEffect.active = false;
        }, 0.15)
        this.MoveSpeed = new Vec3(0, 0, 0);
        tween(this.node).to(0.3, { position: this.StartPos }).call(() => {
            GlobalData.CanClickCar = true;
        }).start();
    }

    /**
     * 更新
     */
    protected update(dt: number): void {

        if (this.MoveSpeed.x != 0 || this.MoveSpeed.y != 0) {
            this.node.setPosition(this.node.getPosition().add(this.MoveSpeed));
            let _pos: Vec3 = this.node.getPosition();


            //判断是否撞到其他位置
            this.CheckHitCar();

            //横向判断
            if (_pos.x < GlobalData.CarBound.left) {
                this.StopUpdateGun();
            } else if (_pos.x > GlobalData.CarBound.right) {
                this.StopUpdateGun();
            }

            //纵向判断
            if (_pos.y < GlobalData.CarBound.bottom) {
                this.StopUpdateGun();
            } else if (_pos.y > GlobalData.CarBound.top) {
                this.StopUpdateGun();
            }

        }
    }

    private CheckHitCar() {
        for (let car of this.node.parent.children) {
            if (car != this.node && car.active && GlobalData.IsHit) {
                let car_width: number = car.getComponent(SingleCar).CheckWidth;
                let car_height: number = car.getComponent(SingleCar).CheckHeight;

                if (Tools.isIntersect(car, this.node, this.CheckWidth + car_width, this.CheckHeight + car_height)) {
                    this.ResetPos();
                    break;
                }
            }
        }
    }

}


